package com.anlu.alspringmvc.demo.action;

import com.anlu.alspringmvc.demo.service.IModifyService;
import com.anlu.alspringmvc.demo.service.IQueryService;
import com.anlu.alspringmvc.spring.framework.annotation.*;
import com.anlu.alspringmvc.spring.framework.webmvc.servlet.ALModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * Description: 发布接口
 * <p>
 *
 * @author Anlu
 * @date 2023/3/9 9:22
 */

@ALController
@ALRequestMapping(value = "/web")
public class MyAction {


    @ALAutowired
    IQueryService queryService;

    @ALAutowired
    IModifyService modifyService;


    @ALRequestMapping("/query.json")
    public ALModelAndView query(HttpServletRequest request, HttpServletResponse response,
                                @ALRequestParam("name") String name){
        String result = queryService.query(name);
        return out(response,result);
    }


    @ALRequestMapping("/add*.json")
    public ALModelAndView add(HttpServletRequest request,HttpServletResponse response,
                              @ALRequestParam("name") String name,@ALRequestParam("addr") String addr){
        String result = modifyService.add(name,addr);
        return out(response,result);
    }

    @ALRequestMapping("/remove.json")
    public ALModelAndView remove(HttpServletRequest request, HttpServletResponse response,
                                 @ALRequestParam("id") Integer id){
        String result = modifyService.remove(id);
        return out(response,result);
    }

    @ALRequestMapping("/edit.json")
    public ALModelAndView edit(HttpServletRequest request,HttpServletResponse response,
                               @ALRequestParam("id") Integer id,
                               @ALRequestParam("name") String name){
        String result = modifyService.edit(id,name);
        return out(response,result);
    }

    private ALModelAndView out(HttpServletResponse resp,String str){
        try {
            resp.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



}
